// pages/user/bindphone.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_info:{},
    cryptStr:'channelRole__I5157922407755d'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
      app.getUserInfo(function(user_info){
           console.log(user_info);
           that.setData({ user_info: user_info});
      })

      if (options.scene) {
        var scene = decodeURIComponent(options.scene);
        this.data.cryptStr = scene;
      }
  },
  bindgetphonenumber: function (e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.iv);
    var that = this;
    if (e.detail.errMsg != "getPhoneNumber:ok") {
      app.alert(e.detail.errMsg);
    } else {
      app.api("api/user/bindwxphone", {
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv,
        wxapp: app.data.wxapp
      }, function (res) {
        if (res.code != 200) {
          app.alert(res.msg);
        } else {
          app.alert("手机验证成功");
          var user_info = that.data.user_info;
          user_info.phone = res.data.phone;
          console.log(res);
          that.setData({ user_info: user_info});
          //wx.navigateBack();
        }
      })
    }
    console.log(e.detail.encryptedData)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  dologin: function () {
    var that = this;
    if (this.data.loading == 1) {
      return false;
    }
    //this.data.loading = 1;
    this.setData({ disabled: 1, loading: 1 })
    console.log(this.data);
    app.api("api/user/dologin", {
      cryptStr: this.data.cryptStr,
    }, function (res) {
      that.setData({ disabled: 0, loading: 0 })
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }

      wx.showToast({
        title: '登陆成功！',
        icon: 'success',
        duration: 2000
      })

      that.setData({ data: res.data });
      var backUrl = wx.getStorage("backUrl");
      if (backUrl){
        wx.navigateTo({
          url: backUrl ,
        })
      }else{
        wx.switchTab({
          url: '/pages/index/index',
        })
      }
      
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})